package com.controller;


import cn.hutool.core.io.FileUtil;
import com.github.pagehelper.PageInfo;
import com.pojo.*;
import com.service.SiteService;
import com.vo.Result;
import com.vo.SiteQuestUserVo;
import com.vo.SiteQuestVo;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Controller
@RequestMapping("/Site")
public class SiteQuestController {
    @Resource
    private SiteService siteService;

    @RequestMapping("/SiteQuest/index")
    public String toSite(){
        return "/Site/SiteQuest";
    }


    @RequestMapping("/SiteQuest/show")
    @ResponseBody
    public Result selectSiteQuest(@RequestBody SiteQuestVo siteQuestVo){
        //显示启用的任务
        siteQuestVo.setQiyong(1);

        PageInfo<Sitequest> sitequestPageInfo = siteService.selectAllSiteQuest(siteQuestVo);
        return Result.success(sitequestPageInfo);
    }

    //查询任务详情
    @RequestMapping("/SiteQuest/showxiangqing")
    @ResponseBody
    public Result selectSiteQuestTwo(Integer id){
        List<Sitepoint> sitepoints = siteService.selectSiteQuestxiangqing(id);
        return Result.success(sitepoints);
    }

    //查询流转对象的下拉框
    @RequestMapping("/SiteQuest/showUser")
    @ResponseBody
    public Result selectSiteProjectUserMc(Integer id){
        Integer ides=1;
        Integer ids=3;
        SiteQuestUserVo siteQuestUserVo=new SiteQuestUserVo();
        siteQuestUserVo.setId(ides);
        siteQuestUserVo.setIds(ids);
        siteQuestUserVo.setIdes(id);
        List<User> users = siteService.selectSiteQuestUser(siteQuestUserVo);
        return Result.success(users);
    }

    @RequestMapping("/SiteQuest/update")
    @ResponseBody
    public Result update(Sitequest sitequest){
        try {
            Siteproject siteproject = new Siteproject();

            siteproject.setId(sitequest.getId());
            siteproject.setUsers(sitequest.getNowpeople());

            siteService.updateSiteProject(siteproject);

            siteService.updateSiteQuest(sitequest);
            return Result.success("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("修改失败");
        }
    }
    @RequestMapping("/SiteQuest/delete")
    @ResponseBody
    public Result delete(Integer id){
        try {
            List<Integer> selectrenwuid = siteService.selectrenwuid(id);
            for (Integer integer : selectrenwuid) {
                    Sitepoint sitepoint=new Sitepoint();
                    sitepoint.setId(integer);
                    sitepoint.setPointstate(1);
                    siteService.updateSitepoint(sitepoint);
            }

            siteService.selectmingxiidbyquestid(id);

            siteService.deleteSiteQuest(id);
            return Result.success("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("删除失败");
        }
    }








    @RequestMapping("/YuangongQuest/index")
    public String toYuangongQuest(){
        return "/Site/YuangongQuest";
    }

    @RequestMapping("/YuangongQuest/show")
    @ResponseBody
    public Result selectYuangongQuest(@RequestBody SiteQuestVo siteQuestVo,HttpServletRequest request){
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        User loginUser = (User) request.getSession().getAttribute("loginUser");

        siteQuestVo.setQiyong(1);

        siteQuestVo.setNowpeople(loginUser.getId());

        PageInfo<Sitequest> sitequestPageInfo = siteService.selectAllSiteQuest(siteQuestVo);
        return Result.success(sitequestPageInfo);
    }

    //查询任务详情
    @RequestMapping("/YuangongQuest/showxiangqing")
    @ResponseBody
    public Result selectYuangongQuestTwo(Integer id){
        List<Sitepoint> sitepoints = siteService.selectSiteQuestxiangqing(id);
        return Result.success(sitepoints);
    }

    //点击任务开始时间和状态
    @RequestMapping("/YuangongQuest/updatetimetype")
    @ResponseBody
    public Result updateYuangongQuestStart(Integer id){
        try {
            Date d = new Date();
            SimpleDateFormat sbf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sbf.format(d);

            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime time = LocalDateTime.now();

            LocalDateTime parse = LocalDateTime.parse(format, df);


            Sitequest sitequest=new Sitequest();
            sitequest.setId(id);
            sitequest.setRealtime(parse);
            sitequest.setState(2);

            siteService.updateSiteQuest(sitequest);
            return Result.success("已开始");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("修改失败");
        }
    }
    //点击任务开始时间和状态
    @RequestMapping("/YuangongQuest/updatetimetypeEnd")
    @ResponseBody
    public Result updateYuangongQuestEen(Integer id){
        try {

            List<Integer> selectrenwuid = siteService.selectrenwuid(id);
            for (Integer integer : selectrenwuid) {
                Integer selectpointstate = siteService.selectpointstate(integer);
                if (selectpointstate == 1){
                    Sitepoint sitepoint=new Sitepoint();
                    sitepoint.setId(integer);
                    sitepoint.setPointstate(4);
                    siteService.updateSitepoint(sitepoint);
                }else if (selectpointstate == 2){
                    Sitepoint sitepoint=new Sitepoint();
                    sitepoint.setId(integer);
                    sitepoint.setPointstate(5);
                    sitepoint.setRealsitetime(" ");

                    siteService.updateSitepoint(sitepoint);
                }
            }


            Date d = new Date();
            SimpleDateFormat sbf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sbf.format(d);

            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime time = LocalDateTime.now();

            LocalDateTime parse = LocalDateTime.parse(format, df);

            Sitequest sitequest=new Sitequest();
            sitequest.setId(id);
            sitequest.setEndrealtime(parse);
            sitequest.setState(3);

            siteService.updateSiteQuest(sitequest);
            return Result.success("已结束");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("修改失败");
        }
    }

    //点击任务详情开始时间和状态
    @RequestMapping("/YuangongQuest/updatetimetypexiangqingStart")
    @ResponseBody
    public Result updateQuestxiangqingStart(Integer id){
        try {
            Date d = new Date();
            SimpleDateFormat sbf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sbf.format(d);

            Sitepoint sitepoint=new Sitepoint();

            sitepoint.setId(id);
            sitepoint.setPointstate(2);
            sitepoint.setRealsitetime(format);

            siteService.updateSitepoint(sitepoint);
            return Result.success("已开始");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("修改失败");
        }
    }


    //点击任务详情开始时间和状态
    @RequestMapping("/YuangongQuest/updatetimetypexiangqingEnd")
    @ResponseBody
    public Result updateQuestxiangqingEnd(Integer id){
        try {
            Date d = new Date();
            SimpleDateFormat sbf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sbf.format(d);

            Sitepoint selectpointbyid = siteService.selectpointbyid(id);

            Sitepoint sitepoint=new Sitepoint();
            sitepoint.setId(id);
            sitepoint.setPointstate(3);
            sitepoint.setRealsitetime(selectpointbyid.getRealsitetime()+"~"+format);

            siteService.updateSitepoint(sitepoint);
            return Result.success("已结束");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("修改失败");
        }
    }



    //任务完成后的报修
    @RequestMapping("/YuangongQuest/addbaoxiu")
    @ResponseBody
    public Result add(Repairs repairs){
        try {
            repairs.setRepstateid(1);
            siteService.addbaoxiu(repairs);
            return Result.success("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("添加失败");
        }
    }


//文件上传
    /**
     * 1.单文件上传
     * @param attach
     * @param request
     * @return
     */
//    @RequestMapping("/YuangongQuestxiangqing/uploads")
//    @ResponseBody
//    public Result uploads(@RequestParam("attach") MultipartFile attach, HttpServletRequest request){
//        //1.获取上传的原始文件名
//        String filename = attach.getOriginalFilename();
//        //2.截取文件名的后缀
//        String suffix = FileUtil.getSuffix(filename);
//        //3.使用UUID生成新的文件名与后缀名进行拼接
//        String newFileName = UUID.randomUUID().toString().replace("-","") + "." + suffix;
//        //4.获取上传到服务器的文件夹的位置
//        String folder = request.getServletContext().getRealPath("/attach");
//        System.out.println(folder);
//        try {
//            //5.将上传的文件拷贝到attach中
//            FileCopyUtils.copy(attach.getBytes(),new File(folder,newFileName));
//            Map<String,Object> map = new HashMap<String,Object>();
//            map.put("attachPath","attach/"+newFileName);
//            return Result.success(map);
//        }catch (Exception e){
//            e.printStackTrace();
//            return Result.success("上传失败");
//        }
//    }

    /**
     * 2.多文件上传
     * @param attach
     * @param request
     * @return
     */
    @RequestMapping("/YuangongQuestxiangqing/uploadMore")
    @ResponseBody
    public Result uploadMore(@RequestParam("attach") MultipartFile[] attach, HttpServletRequest request){

        Map<String,Object> map = new HashMap<String,Object>();
        List<String> list = new ArrayList<String>();
        //4.获取上传到服务器的文件夹的位置
        String folder = request.getServletContext().getRealPath("/attach");

        for (MultipartFile file : attach) {
            //1.获取上传的原始文件名
            String filename = file.getOriginalFilename();
            //2.截取文件名的后缀
            String suffix = FileUtil.getSuffix(filename);
            //3.使用UUID生成新的文件名与后缀名进行拼接
            String newFileName = UUID.randomUUID().toString().replace("-","") + "." + suffix;
            try {
                //5.将上传的文件拷贝到attach中
                FileCopyUtils.copy(file.getBytes(),new File(folder,newFileName));
                list.add("attach/"+newFileName);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        map.put("attachPath",list);
        return Result.success(map);
    }

    //修改数据库
    @RequestMapping("/YuangongQuestxiangqing/updatePath")
    @ResponseBody
    public Result updatePath(Sitepoint sitepoint){
        try {
            System.out.println(sitepoint);
            siteService.updateSitepoint(sitepoint);
            return Result.success("上传成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("上传失败");
        }
    }



}




